package com.jiwanqing.demo.util;

import java.sql.*;

import static com.mysql.cj.conf.PropertyKey.PASSWORD;

public class JDBCUtil {
    public static String URL = "jdbc:mysql://localhost:3306/demo?characterEncoding=utf8&useUnicode=true";
    public static String USERNAME = "root";
    public static String PASSWORD = "123456";

    static{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("请检查数据库的jar包");
            e.printStackTrace();
        }
    }

    public static Connection getCon() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            System.out.println("获取数据库连接失败");
            e.printStackTrace();
        }
        return con;
    }

    public static void  closeAll(ResultSet res, Statement stmt, Connection con) {
        if(res != null) {
            try {
                res.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if(con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
